home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tricks of the Mac Game Programming Gurus
/
TricksOfTheMacGameProgrammingGurus.iso
/
More Source
/
C⁄C++
/
CopyBits & CopyMask
/
CopyBits meets CopyMask.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-03-13
|
4KB
|
146 lines
#include <QDOffScreen.h>
/* Standard inits */
#define kLoopNumber 100
static void InitToolbox(void)
{
InitGraf (&qd.thePort);
InitFonts ();
FlushEvents (everyEvent,0);
InitWindows ();
InitMenus ();
TEInit ();
InitDialogs (nil);
InitCursor ();
} /*InitToolbox*/
void main(void)
{
WindowPtr myWindow;
Rect windowRectangle;
short h,v;
GrafPtr screenPort;
GDHandle screenDevice;
Rect pictRectangle, srcRectangle;
PicHandle coolPicture, maskPicture;
CTabHandle cTable;
GrafPtr offscreenGWorld = nil, maskGWorld = nil;
RgnHandle maskRegion;
// offscreenGWorld is really a GWorldPtr rather than a GrafPtr, but I prefer to
// refer to them by GrafPtrs, since the CopyBits calls get much cleaner.
/* Variables for loops, timing and display */
long beforeTime, afterTime;
long loop;
Str255 tempStr;
InitToolbox();
/* Load the picture*/
coolPicture = GetPicture(128); /*PICT resource #128.*/
if (coolPicture == nil)
{
SysBeep(1);
ExitToShell();
}
maskPicture = GetPicture(129); /*PICT resource #129.*/
if (maskPicture == nil)
{
SysBeep(1);
ExitToShell();
}
/* Get its frame */
pictRectangle = (**coolPicture).picFrame;
/* Move it to 0,0 */
OffsetRect(&pictRectangle, -pictRectangle.left, -pictRectangle.top);
/*Set up the window*/
windowRectangle = pictRectangle;
OffsetRect(&windowRectangle, 64, 64);
windowRectangle.bottom += 100;
myWindow = NewCWindow(0L, &windowRectangle, "\pCopyBitsSpeedTest", true, 0, (WindowPtr)-1L, false, 0);
SetPort(myWindow);
// DrawPicture(coolPicture, &pictRectangle);
/* Remember it so we can get back after visiting a GWorld */
GetGWorld((GWorldPtr *)&screenPort, &screenDevice);
/* Create a GWorld to draw the PICT in */
if ( noErr != NewGWorld((GWorldPtr *)&offscreenGWorld, 0, &pictRectangle, nil, nil, 0) )
ExitToShell();
/*We lock the offscreen pixmap so we can CopyBits and PlotCIcon to it.*/
if ( LockPixels(((CGrafPtr)offscreenGWorld)->portPixMap) )
;
SetGWorld((GWorldPtr)offscreenGWorld, nil);
PaintRect(&pictRectangle);
DrawPicture(coolPicture, &pictRectangle);
/* Create a GWorld to draw the mask in */
if ( noErr != NewGWorld((GWorldPtr *)&maskGWorld, 1, &pictRectangle, nil, nil, 0) )
ExitToShell();
/*We lock the offscreen pixmap so we can CopyBits and PlotCIcon to it.*/
if ( LockPixels(((CGrafPtr)maskGWorld)->portPixMap) )
;
SetGWorld((GWorldPtr)maskGWorld, nil);
EraseRect(&pictRectangle);
DrawPicture(maskPicture, &pictRectangle);
maskRegion = NewRgn();
BitMapToRegion(maskRegion, &(maskGWorld->portBits));
/* GWorlds done. Set back port and device! */
SetGWorld((GWorldPtr)screenPort, screenDevice);
// CopyBits(&offscreenGWorld->portBits, &myWindow->portBits, &pictRectangle, &pictRectangle, srcCopy, nil);
// CopyBits
srcRectangle = pictRectangle;
beforeTime = TickCount();
for (loop = 0; loop < kLoopNumber; loop++)
{
srcRectangle = pictRectangle;
OffsetRect(&srcRectangle, 0, loop % 20 - 10);
CopyBits(&offscreenGWorld->portBits, &myWindow->portBits, &srcRectangle, &pictRectangle, srcCopy, maskRegion);
}
afterTime = TickCount();
MoveTo(10, windowRectangle.bottom - 80 - 64);
NumToString(afterTime - beforeTime, tempStr);
DrawString("\pCopyBits: ");
DrawString(tempStr);
DrawString("\p ticks.");
beforeTime = TickCount();
for (loop = 0; loop < kLoopNumber; loop++)
{
srcRectangle = pictRectangle;
OffsetRect(&srcRectangle, 0, loop % 20 - 10);
CopyMask(&offscreenGWorld->portBits, &maskGWorld->portBits, &myWindow->portBits, &srcRectangle, &pictRectangle, &pictRectangle);
}
afterTime = TickCount();
MoveTo(10, windowRectangle.bottom - 50 - 64);
NumToString(afterTime - beforeTime, tempStr);
DrawString("\pCopyMask: ");
DrawString(tempStr);
DrawString("\p ticks.");
while (!Button());
} /*main*/